package com.example.longimageView;

import android.app.Application;

import com.blankj.utilcode.util.LogUtils;
import com.example.longimageView.db.UserDataBaseBean;
import com.example.longimageView.db.WireLessDataBaseBean;
import com.example.longimageView.utils.FireGsonUtil;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * @author: njb
 * @date: 2023/5/10 23:08
 * @desc:
 */
public class App extends Application {
    private static App instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        x.Ext.init(this);
        x.Ext.setDebug(true);
    }

    private static DbManager dbManager;

    /**
     * 获取数据库配置信息
     *
     * @return 配置信息对象
     */
    public DbManager getLocalDbManager() {
        if (dbManager == null) {
            DbManager.DaoConfig dbConfig = new DbManager.DaoConfig()
                    .setDbName("local.db")
                    .setDbVersion(7)
                    .setDbOpenListener(db -> db.getDatabase().enableWriteAheadLogging()) //监听数据库打开
                    .setDbUpgradeListener((db, oldVersion, newVersion) -> { //监听数据库更新
                        //todo：数据库升级需要做的一些操作
                            try {
                                if(oldVersion != newVersion){
                                    db.addColumn(UserDataBaseBean.class,"mac");
                                    List<UserDataBaseBean> userList =  db.findAll(UserDataBaseBean.class);
                                   // db.update(UserDataBaseBean.class);
                                    db.saveOrUpdate(userList);
                                    LogUtils.d("--数据库版本升级--", FireGsonUtil.listToJson(userList));
                                }
                            } catch (DbException e) {
                                e.printStackTrace();
                            }
                    }).setTableCreateListener((db, table) -> {

                    });
            dbManager = x.getDb(dbConfig);
        }
        return dbManager;
    }

    public static App getInstance() {
        return instance;
    }
}
